class Admin::TranslateController < ApplicationController
  before_filter :check_admin_authorization
  layout "admin"

def index
   @un = false
   if request.post?
   @en = params[:filter][:english]
   @ch = params[:filter][:chinese]
   @un = true if params[:commit].to_s == "Untranslated"
   else
   @en = params[:en]
   @ch = params[:ch]
   @un = true if params[:un].to_s == "true"
   end

   if @en.to_s != ""
    @view_pages, @view_translations = paginate :ViewTranslation,
                     :per_page=>20, :conditions => [ "language_id = ? and tr_key like '%#{@en}%' ", Locale.language.id ], 
		     :order => 'id DESC'
   elsif @ch.to_s != ""
    @view_pages, @view_translations = paginate :ViewTranslation,
                     :per_page=>20, :conditions => [ "language_id = ? and text like '%#{@ch}%'", Locale.language.id ], 
		     :order => 'id DESC'
   elsif @un
    @view_pages, @view_translations = paginate :ViewTranslation,
                     :per_page=>20, :conditions => [ 'language_id = ? and text is null', Locale.language.id ], 
		     :order => 'id DESC'
   else
    @view_pages, @view_translations = paginate :ViewTranslation,
                     :per_page=>20, :conditions => [ 'language_id = ?', Locale.language.id ], 
		     :order => 'id DESC'
   end
  end

  def translation_text
    @translation = ViewTranslation.find(params[:id])
    render :text => @translation.text || ""  
  end

  def set_translation_text
    @translation = ViewTranslation.find(params[:id])
    previous = @translation.text
    @translation.text = params[:value]
    @translation.text = previous unless @translation.save
    render :partial => "translation_text", :object => @translation.text  
  end
  def delete
    tr = ViewTranslation.find(params[:id])
	tr.destroy
	redirect_to "/translate?page=" + params[:page].to_s
  end

end
